package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {


 @Override
 public R<ShoppingCart> add(ShoppingCart shoppingCart) {
  String dishFlavor = shoppingCart.getDishFlavor();
  //获取用户ID
  Long userId = BaseContext.getCurrentId();
  //设置用户ID
  shoppingCart.setUserId(userId);
  //获取菜品ID
  Long dishId = shoppingCart.getDishId();

  //构造查询条件
  LambdaQueryWrapper<ShoppingCart> dqw = Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, userId);

  if (dishId != null) {
   //添加到购物车的是菜品
   dqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
  } else {
   //添加到购物车的是套餐
   dqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
  }
  //查询当前菜品或者套餐是否在购物车中
  //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
  ShoppingCart shoppingCart1 = this.getOne(dqw);
  //如果购物车中已存在，就把数量加1
  if (shoppingCart1 != null) {
   Integer number = shoppingCart1.getNumber();
   number++;
   shoppingCart1.setNumber(number);
   this.updateById(shoppingCart1);
  } else {
   //如果不存在，则添加到购物车，数量默认就是一
   shoppingCart.setNumber(1);
   shoppingCart.setCreateTime(LocalDateTime.now());
   this.save(shoppingCart);
   shoppingCart1 = shoppingCart;
  }
  return R.success(shoppingCart1);
 }

 @Override
 public R<List<ShoppingCart>> list1() {
  log.info("查看购物车...");

  LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
  queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
  queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

  List<ShoppingCart> list = this.list(queryWrapper);

  return R.success(list);
 }

 @Override
 public R<ShoppingCart> sub(ShoppingCart shoppingCart) {
  Long userId = BaseContext.getCurrentId();
  //设置用户ID
  shoppingCart.setUserId(userId);
  //获取菜品ID
  Long dishId = shoppingCart.getDishId();

  //构造查询条件
  LambdaQueryWrapper<ShoppingCart> dqw = Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, userId);

  if (dishId != null) {
   //添加到购物车的是菜品
   dqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
  } else {
   //添加到购物车的是套餐
   dqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
  }
  //查询当前菜品或者套餐是否在购物车中
  //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
  ShoppingCart shoppingCart1 = this.getOne(dqw);
  //如果购物车中已存在，就把数量减1
  if (shoppingCart1 != null) {
   Integer number = shoppingCart1.getNumber();
   number--;
   shoppingCart1.setNumber(number);
   this.updateById(shoppingCart1);
   if (number == 0) {
    this.removeById(shoppingCart1);
   }
  }
  return R.success(shoppingCart1);

 }

 @Override
 public void delete() {
  Long userId = BaseContext.getCurrentId();
  LambdaQueryWrapper<ShoppingCart> dqw = Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, userId);
  this.remove(dqw);
 }
}
